package gs.sr.tkfxz.shared;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * FieldVerifier validates that the name the user enters is valid.
 * </p>
 * <p>
 * This class is in the <code>shared</code> package because we use it in both
 * the client code and on the server. On the client, we verify that the name is
 * valid before sending an RPC request so the user doesn't have to wait for a
 * network round trip to get feedback. On the server, we verify that the name is
 * correct to ensure that the input is correct regardless of where the RPC
 * originates.
 * </p>
 * <p>
 * When creating a class that is used on both the client and the server, be sure
 * that all code is translatable and does not use native JavaScript. Code that
 * is not translatable (such as code that interacts with a database or the file
 * system) cannot be compiled into client side JavaScript. Code that uses native
 * JavaScript (such as Widgets) cannot be run on the server.
 * </p>
 */
public class FieldVerifier {
	private static final List<Long> taobaoItemIds = new ArrayList<Long>();
	private static final List<Long> tmallItemIds = new ArrayList<Long>();
	private static final Map</* item url prefix */String, /* itemId */Long> supportedItemUrlMap = new HashMap<String, Long>();
	static {
		/**
		 * TODO add ad item here
		 */
		taobaoItemIds.add(0L);
		tmallItemIds.add(0L);

		for (Long currItemId : taobaoItemIds) {
			supportedItemUrlMap.put("http://item.taobao.com/item.htm?",
					currItemId);
		}
		for (Long currItemId : tmallItemIds) {
			supportedItemUrlMap.put("http://detail.tmall.com/item.htm?",
					currItemId);
		}
	}

	public static boolean isValidUserNick(String nick) {
		return nick != null && nick.trim().length() > 2;
	}

	/**
	 * Verifies that the specified name is valid for our service.
	 * 
	 * In this example, we only require that the name is at least four
	 * characters. In your application, you can use more complex checks to
	 * ensure that usernames, passwords, email addresses, URLs, and other fields
	 * have the proper syntax.
	 * 
	 * @param url
	 *            the name to validate
	 * @return true if valid, false if invalid
	 */
	public static boolean isValidItemUrl(String url) {
		return getItemId(url) != null;
	}

	public static Long getItemId(String url) {
		Long itemId = null;
		if (url != null) {
			url = url.trim();
			for (String page : supportedItemUrlMap.keySet()) {
				if (url.startsWith(page)) {
					String[] params = url.substring(page.length()).split("&");
					if (params != null && params.length > 0) {
						for (String param : params) {
							String[] kv = param.split("=");
							if (kv != null && kv.length == 2) {
								if ("id".equals(kv[0])) {
									try {
										long iid = Long.parseLong(kv[1]);
										if (iid > 0) {
											itemId = Long.valueOf(iid);
											break;
										}
									} catch (Exception e) {
										//
									}
								}
							}
						}
					}

				}
			}
		}
		return itemId;
	}

	public static Map<String, Long> getSupporteditemurlmap() {
		return supportedItemUrlMap;
	}

}
